package com.zhuiyun.pm.sys.user.controller;

import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.result.CommonResult;
import com.zhuiyun.pm.sys.user.mapper.SysUserMapper;
import com.zhuiyun.pm.sys.user.entity.SysUserEntity;
import com.zhuiyun.pm.sys.user.service.SysUserService;
import com.zhuiyun.pm.utils.Md5Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * @Author 赵康成
 * @Description 对用户表做出操作
 * @Date  2023-03-13 10:12:19
 * @Return
 */
@RestController
@CrossOrigin
@RequestMapping("/sysUser")
public class SysUserController {

	@Autowired private SysUserService sysUserService;
	@Resource
	private SysUserMapper sysUserDao;

   /*    * @Title: 登录
    	 * @Description: 通过用户名密码进行登录
    	 * @param: @return
    	 * @return:loginName,userPassword
    	 * @author:  zkc
    	 * @date: 2023/3/13 10:13
    	 * @version V1.8
    	 */
	@PostMapping("/login")
	public CommonResult login(@RequestBody SysUserEntity sysUserEntity){

		String loginName = sysUserEntity.getLoginName();
		String password = sysUserEntity.getPassword();

		//0.判断用户名和密码不能为空
		if (StringUtils.isBlank(loginName) ||
				StringUtils.isBlank(password)) {
			return CommonResult.error("用户名密码不能为空");
		}

		//1.实现登录
		SysUserEntity userResult = sysUserService.queryUserForLogin(loginName, Md5Util.getMd5DigestAsHex(password));

		if(userResult == null){
			return CommonResult.error("用户名或密码不正确");
		}
		// 创建时间
		Date createTime = new Date();
		// 时间格式初始化
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String createTimeSdf = sdf.format(createTime);
		String token = Base64Utils.encodeToString((sysUserEntity.getLoginName()+createTimeSdf+userResult.getPassword()).getBytes());
		userResult.setToken(token);
		return CommonResult.ok(userResult);
	}

	/**
	* @Description 分页获取后台用户管理信息列表
	* @Author lzy
	* @Date 2023/3/14 18:42
	* @Return com.zhuiyun.pm.result.CommonResult
	* @param pageRequest
	* @param loginName
	* @param nickName
	*/
	@GetMapping("/getSysUserListAll")
	public CommonResult getSysUserListAll(PageRequest pageRequest, String loginName, String nickName){
		return sysUserService.getSysUserListAll(pageRequest,loginName,nickName);
	}

	/**
	* @Description 根据id删除用户信息
	* @Author lzy
	* @Date 2023/3/14 19:12
	* @Return com.zhuiyun.pm.result.CommonResult
	* @param id
	*/
	@PutMapping("/deleteSysUserById")
	public CommonResult deleteSysUserById(Integer id) {
		return sysUserService.deleteSysUserById(id);
	}

	/**
	* @Description 新增用户信息
	* @Author lzy
	* @Date 2023/3/14 19:14
	* @Return com.zhuiyun.pm.result.CommonResult
	* @param multipartRequest
	* @param sysUserEntity
	*/
	@PostMapping("/saveSysUser")
	public CommonResult saveSysUser(MultipartRequest multipartRequest, SysUserEntity sysUserEntity){
		return sysUserService.saveSysUser(multipartRequest,sysUserEntity);
	}
	
	/**
	* @Description 校验登录名称是否已存在
	* @Author lzy
	* @Date 2023/3/14 20:27
	* @Return com.zhuiyun.pm.result.CommonResult
	* @param loginName
	*/
	@GetMapping("/checkLoginName")
	public CommonResult checkLoginName(String loginName){
		return sysUserService.checkLoginName(loginName);
	}


}
